package com.example.test.buffer;

import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author: fxy
 * @Date: 2022/01/07/10:45
 * @Description:
 */
public class Demo1 {
    @Test
    public void buffer01() throws Exception {
        RandomAccessFile aFile = new RandomAccessFile("D:\\project\\myproject\\test\\test07-nio\\src\\main\\resources\\file\\01.txt","rw");
        FileChannel channel = aFile.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(1024);

        int byteRead = channel.read(buffer);
        while(byteRead!=-1){
            buffer.flip();
            while (buffer.hasRemaining()){
                System.out.println((char)buffer.get());
            }
            buffer.clear();
            byteRead = channel.read(buffer);
        }

    }

    @Test
    public void buffer02(){
        IntBuffer buffer = IntBuffer.allocate(8);
        for(int i = 0;i<buffer.capacity();i++){
            int j = 2*(i+1);
            buffer.put(j);
        }
        buffer.flip();
        while(buffer.hasRemaining()){
            final int val = buffer.get();
            System.out.println(val+" ");
        }
    }
}
